<%--
  Created by IntelliJ IDEA.
  User: 恒少
  Date: 2021/3/18
  Time: 14:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>书籍类型</title>
</head>
<link rel="stylesheet" href="layui/css/layui.css">
<script src="layui/layui.js"></script>
<!-- L2Dwidget.js L2D网页动画人物 -->
<script src="https://eqcn.ajz.miesnfu.com/wp-content/plugins/wp-3d-pony/live2dw/lib/L2Dwidget.min.js"></script>
<script>
    L2Dwidget.init({
        "model": { "jsonPath":"https://unpkg.com/live2d-widget-model-koharu@1.0.5/assets/koharu.model.json", "scale": 1, "hHeadPos":0.5, "vHeadPos":0.618 },
        "display": { "position": "right", "width": 250, "height": 300, "hOffset":0, "vOffset": 0 },
        "mobile": { "show": true, "scale": 0.5 },
        "react": { "opacityDefault": 0.7, "opacityOnHover": 0.2 }
    });
</script>
<body>
<div style="margin: 0 43% ;color: #4E5465;font-size: 50px " >类别信息</div>

<table class="layui-hide" id="test" lay-filter="test"></table>


<form class="layui-form " id="formData" style="display: none" lay-filter="formData">
    <input type="hidden" name="typeid">
    <div class="layui-form-item">
        <label class="layui-form-label">类别</label>
        <div class="layui-input-inline">
            <input type="text" name="type" placeholder="请输入类别名" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <button class="layui-btn" lay-submit="" lay-filter="submitForm">提交</button>
    </div>
</form>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="addType">新增</button>
    </div>
</script>
<%--显示订单详情的弹窗--%>
<div id="up" style="display: none">
    <table class="layui-hide" id="test1" lay-filter="test1"></table>
</div>


<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">查看详情</a>
</script>



<script>
    layui.use(['table', 'jquery', 'form', 'layer'], function () {
        var table = layui.table;
        var $ = layui.jquery;
        var form = layui.form;
        var layer = layui.layer;

       $(function () {
            showTable();
       });

        function showTable(){
            table.render({
                elem: '#test'
                ,url:'${pageContext.request.contextPath}/book/getAllBookType'
                ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                // ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                //     title: '提示'
                //     ,layEvent: 'LAYTABLE_TIPS'
                //     ,icon: 'layui-icon-tips'
                // }]
                ,title: '用户数据表'
                ,cols: [[
                    {type: 'checkbox', fixed: 'left'}
                    ,{field:'typeid', title:'ID', width:80, fixed: 'left', unresize: true, sort: true}
                    ,{field:'type', title:'类型'}
                    ,{field:'sum(count)', title:'数量'}
                    ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
                ]]
            });

        }


        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'addType':
                    var data = checkStatus.data;
                    //清空
                    $("#formData")[0].reset();
                    layer.open({
                        type: 1
                        , title: '新增书籍'
                        , content: $('#formData')
                        , anim: 1
                    });

                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选');
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    obj.del();
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){

                layer.open({
                    type: 1,
                    title: "标题",
                    fixed: true,
                    resize: false,
                    moveOut: true,
                    totalRow: true,
                    offset: 'auto',
                    area: ['950px', '400px'],
                    content: $('#up'),
                    success: function () {
                        table.render({
                            elem: '#test1'
                            , url: '${pageContext.request.contextPath}/book/queryBookByTypeid?typeid='+data.typeid
                            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                            , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                                title: '提示'
                                , layEvent: 'LAYTABLE_TIPS'
                                , icon: 'layui-icon-tips'
                            }]
                            , title: '用户数据表'
                            , cols: [[
                                {type: 'checkbox', fixed: 'left'}
                                ,{field: 'bookid', title: '书ID', fixed: 'left', unresize: true, sort: true}
                                , {field: 'bookname', title: '书名', edit: 'text'}
                                , {field: 'author', title: '作者', edit: 'text', sort: true}
                                , {field: 'count', title: '数量', edit: 'text'}
                                , {field: 'press', title: '出版社', sort: true}
                                , {field: 'state', title: '状态', sort: true,templet: '#titleTpl'}
                                , {field: 'outprice', title: '售价', sort: true}
                                , {field: 'inprice', title: '进价', sort: true}
                                , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150}
                            ]]
                        });

                    }
                });

            }
        });


        <%--//监听表单提交事件--%>
        <%--form.on('submit(submitForm)', function(data){--%>
            <%--$.ajax({--%>
                <%--type:'post',--%>
                <%--date:data.field,--%>
                <%--url:'${pageContext.request.contextPath}/book/addType?type='+data.type,--%>
                <%--dateType:'json',--%>
                <%--success:function (result) {--%>
                    <%--if(result.code==1){--%>
                        <%--layer.msg(result.msg,{icon:1})--%>
                        <%--showTable();--%>
                    <%--}else {--%>
                        <%--layer.msg(result.msg,{icon:5})--%>
                    <%--}--%>
                <%--}--%>
            <%--});--%>
            <%--return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。--%>
        <%--});--%>


        form.on('submit(submitForm)', function (data) {
            $.ajax({
                type: 'post',
                url: '${pageContext.request.contextPath}/book/addType',
                data: JSON.stringify(data.field),
                contentType: 'application/json;charset=utf-8',
                success: function (result) {
                    layer.closeAll();
                    if (result.code == 1) {
                        //layer.msg  提示框
                        showTable();
                        layer.msg(result.msg);
                    } else {
                        layer.msg(result.msg);
                    }

                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
    });
</script>



</body>
</html>
